﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace StockDashboard.DL.Yahoo
{
    [DataContract]
    public class QuoteInfo
    {
        private DateTime _date;
        private double _open, _high, _low, _close, _adj;
        private ulong _volume;
        private String _symbol;

        public QuoteInfo(String symbol, DateTime date, double open, double high, double low,
            double close, ulong volume, double adj)
        {
            _symbol = symbol;
            _date = date;
            _open = open;
            _high = high;
            _low = low;
            _close = close;
            _volume = volume;
            _adj = adj;
        }
        public QuoteInfo(string line, String symbol)
        {
            string[] fields = line.Split(new char[] { ',' });

            if (fields != null && fields.Length > 5)
            {
                _date = DateTime.Parse(fields[0]);
                _open = Double.Parse(ConvertToLocal(fields[1]));
                _high = Double.Parse(ConvertToLocal(fields[2]));
                _low = Double.Parse(ConvertToLocal(fields[3]));
                _close = Double.Parse(ConvertToLocal(fields[4]));
                _volume = ulong.Parse(fields[5]);
                _adj = Double.Parse(ConvertToLocal(fields[6]));
                _symbol = symbol;
            }
        }

        public QuoteInfo()
        {

        }
        [DataMember()]
        public DateTime Date
        {
            get
            {
                return _date;
            }
            set
            {
                _date = value;
            }
        }
        [DataMember()]
        public double Open
        {
            get
            {
                return _open;
            }
            set
            {
                _open = value;
            }
        }
        [DataMember()]
        public double High
        {
            get
            {
                return _high;
            }
            set
            {
                _high = value;
            }
        }

        [DataMember()]
        public double Low
        {
            get
            {
                return _low;
            }
            set
            {
                _low = value;
            }
        }
        [DataMember()]
        public double Close
        {
            get
            {
                return _close;
            }
            set
            {
                _close = value;
            }
        }
        [DataMember()]
        public double Adj_Close
        {
            get
            {
                return _adj;
            }
            set
            {
                _adj = value;
            }
        }
        [DataMember()]
        public ulong Volume
        {
            get
            {
                return _volume;
            }
            set
            {
                _volume = value;
            }
        }

        [DataMember()]
        public String Symbol
        {
            get
            {
                return _symbol;
            }
            set
            {
                _symbol = value;
            }
        }

        public int CompareTo(QuoteInfo item)
        {
            return _date.CompareTo(item.Date);
        }
        public QuoteInfo Clone()
        {
            return new QuoteInfo(_symbol, _date, _open, _high, _low, _close, _volume, _adj);
        }
        public override string ToString()
        {
            return String.Format("{0},{1},{2},{3},{4},{5},{6}",
                _date, _open, _high, _low, _close, _volume, _adj);
        }

        private string ConvertToLocal(string str)
        {
            string mark = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
            if (mark == ".")
                return str.Replace(",", ".");
            else
                return str.Replace(".", ",");
        }
    }
}
